package top.ewind.interceptor;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.redis.Redis;

import top.ewind.controller.common.RedisConstants;

/**
 * @author wangchao
 *
 */
public class AppInterceptor implements Interceptor {

	/* (non-Javadoc)
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	@Override
	public void intercept(Invocation inv) {
		String usertoken = inv.getController().getPara("usertoken");
		if(StrKit.notBlank(usertoken)&&usertoken.length()>16){
			String userid = usertoken.substring(0,16);
			String rtoken = Redis.use().get(RedisConstants.USER_TOKEN+userid);
			if(rtoken.equals(usertoken)){
				inv.getController().setAttr("userid",userid);
				inv.invoke();
				return;
			}
		}
		Map<String, Object> res = new HashMap<String, Object>(); 
		res.put("ret",400003);
		res.put("msg","usertoke无效获已过期");
		inv.getController().renderJson(res);
	}

}
